package com.easy.work.function.mall.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.syslib.util.MessageTools;
import com.easy.work.R;
import com.easy.work.bean.MallGoodsBean;
import com.easy.work.function.mall.bean.NeedIntegralEvent;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhangfeng
 * @date 2017/12/20
 * @desc 提交记录适配器
 */

public class CommitOrderAdapter extends ArrayAdapter<MallGoodsBean> {

    private List<MallGoodsBean> mList;
    private LayoutInflater mInflater;
    private Context mContext;

    public int getNumber() {
        return mNumber;
    }

    private int mNumber = 1;

    public CommitOrderAdapter(Context context, int resource, List<MallGoodsBean> objects) {
        super(context, resource, objects);
        mList = objects;
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    /**
     * 刷新数据
     */
    public void refreshData(List<MallGoodsBean> list) {
        mList.clear();
        addMoreData(list);
    }

    /**
     * 添加更多数据
     */
    private void addMoreData(List<MallGoodsBean> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_order, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final MallGoodsBean bean = mList.get(position);
        holder.tvName.setText(bean.goodsName);
        Picasso.with(mContext).load(bean.goodsImageX).into(holder.ivGoods);
        holder.tvPrice.setText(bean.integral + "积分");
        holder.tvNumber.setText("1");
        holder.tvDecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number = Integer.parseInt(holder.tvNumber.getText().toString());
                if (number == 1) {
                    return;
                }
                number--;
                mNumber = number;
                holder.tvNumber.setText(String.valueOf(number));
                if (number == 1) {
                    holder.tvDecrease.setBackgroundResource(R.drawable.jian_hui);
                }
                EventBus.getDefault().post(new NeedIntegralEvent(String.valueOf(number * bean.integral)));
            }
        });
        holder.tvIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int number = Integer.parseInt(holder.tvNumber.getText().toString());
                if (number >= bean.inventory) {
                    MessageTools.showToast(mContext, "只剩下当前库存");
                    return;
                }
                number++;
                mNumber = number;
                holder.tvNumber.setText(String.valueOf(number));
                if (number == 2) {
                    holder.tvDecrease.setBackgroundResource(R.drawable.jian);
                }
                EventBus.getDefault().post(new NeedIntegralEvent(String.valueOf(number * bean.integral)));
            }
        });
        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.iv_goods)
        ImageView ivGoods;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_spec)
        TextView tvSpec;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tv_decrease)
        TextView tvDecrease;
        @BindView(R.id.tv_increase)
        TextView tvIncrease;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
